package zhang.automobileservice.activity.firstpagedetial;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import adapter.MaintenanceAdapter;
import bean.Maintain_Combo;
import utils.MyUrl;
import utils.RequestQueueUtils;
import zhang.automobileservice.FarterActivity;
import zhang.automobileservice.MaintainDetailActivity;
import zhang.automobileservice.R;

public class MaintainActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private TextView tv_01;
    private ImageView iv_up01;
    private ImageView iv_down01;
    private TextView tv_02;
    private ImageView iv_up02;
    private ImageView iv_down02;
    private TextView tv_03;
    private boolean pricesort = true;
    private boolean numsort = true;
    private PullToRefreshListView pull_to_refresh_list;
    private ArrayList<Maintain_Combo> mainList;
    private MaintenanceAdapter adapter;
    private int count=1;
    private int Order=1;
    private String url;
    private RelativeLayout rl_01;
    private RelativeLayout rl_02;
    private RelativeLayout rl_03;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain);
        mainList = new ArrayList<>();
        initView();
        adapter = new MaintenanceAdapter(getApplicationContext(), mainList);
        pull_to_refresh_list.setAdapter(adapter);
        getData();
    }

    private void getData() {
        url = MyUrl.Font_Url + MyUrl.queryPage + Order + "&page=" + count;
        if (count == 1) {
            mainList.clear();
        }
        RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    if ("0".equals(o.getString("code"))) {
                        JSONArray array = o.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            Maintain_Combo maintainCombo = new Maintain_Combo();
                            JSONObject object = (JSONObject) array.get(i);
                            maintainCombo.setId(object.getInt("id") + "");
                            maintainCombo.setName(object.getString("name"));
                            maintainCombo.setCom_location(object.getString("com_location"));
                            maintainCombo.setOriginaPrice(object.getInt("originaPrice") + "");
                            maintainCombo.setPrice(object.getInt("price") + "");
                            maintainCombo.setCount(object.getInt("count") + "");
                            maintainCombo.setImage(object.getString("image"));
                            mainList.add(maintainCombo);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter.notifyDataSetChanged();
                pull_to_refresh_list.onRefreshComplete();
                pull_to_refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        pull_to_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                count = 1;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                count++;
                getData();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.rl_01:
                if(pricesort == true) {
                count = 1;
                Order = 2;
                iv_down01.setImageResource(R.mipmap.downarrow_gray);
                iv_up01.setImageResource(R.mipmap.up_light);
                getData();
                pricesort = false;
            } else {
                count = 1;
                Order = 1;
                iv_down01.setImageResource(R.mipmap.downarrow_blue);
                iv_up01.setImageResource(R.mipmap.up_grey);
                getData();
                pricesort = true;
            }
                break;
            case R.id.rl_02:
                if (numsort == true) {
                    Order = 4;
                    count = 1;
                    iv_down02.setImageResource(R.mipmap.downarrow_gray);
                    iv_up02.setImageResource(R.mipmap.up_light);
                    getData();
                    numsort = false;
                } else {
                    Order = 3;
                    count = 1;
                    iv_down02.setImageResource(R.mipmap.downarrow_blue);
                    iv_up02.setImageResource(R.mipmap.up_grey);
                    getData();
                    numsort = true;
                }
                break;
            case R.id.rl_03:
                break;
        }
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        tv_01 = (TextView) findViewById(R.id.tv_01);
        iv_up01 = (ImageView) findViewById(R.id.iv_up01);
        iv_down01 = (ImageView) findViewById(R.id.iv_down01);
        tv_02 = (TextView) findViewById(R.id.tv_02);
        iv_up02 = (ImageView) findViewById(R.id.iv_up02);
        iv_down02 = (ImageView) findViewById(R.id.iv_down02);
        tv_03 = (TextView) findViewById(R.id.tv_03);
        pull_to_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_list);
        finish.setOnClickListener(this);
        rl_01 = (RelativeLayout) findViewById(R.id.rl_01);
        rl_01.setOnClickListener(this);
        rl_02 = (RelativeLayout) findViewById(R.id.rl_02);
        rl_02.setOnClickListener(this);
        rl_03 = (RelativeLayout) findViewById(R.id.rl_03);
        rl_03.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pull_to_refresh_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("点击了条目", "点击了条目");
                Intent intent = new Intent(getApplicationContext(), MaintainDetailActivity.class);
                intent.putExtra("id", mainList.get(position - 1).getId());
                startActivity(intent);
            }
        });
    }
}
